﻿$(function () {
    toastr.options.positionClass = 'toast-bottom-center';
    var vm = new Vue({
        el: "#app",
        data: {
            filterName: '',
            dialogVisible: false,
            dialogTitle: '新建菜品类型',
            tableData: [],
            formLabelWidth: '120px',
            actionType: '新建',
            foodType: {
                id: null,
                name: null,
                description:null
            },
            rules: {
                name: [
                    { required: true, message: '请输入科室名', trigger: 'blur' }
                ]
            }
        },
        methods: {
            // 加载列表数据
            loadTableData() {
                var vm = this;
                abp.ajax({
                    type: 'GET',
                    url: `/api/food-type/get-list?filterName=${vm.filterName}`
                }).then((res) => {
                    if (res.code === 0) {
                        vm.tableData = res.data;
                    }
                })
            },

            // 删除
            deleteFoodType(index, row) {
                var vm = this;
                abp.message.confirm(
                    `确定要删除 ${row.name}?`,
                    '删除菜品类型',
                    (confirmed) => {
                        if (!confirmed) {
                            return;
                        }
                        else {
                            abp.ajax({
                                type: 'GET',
                                url: `/api/food-type/delete?id=${row.id}`
                            }).then((res) => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.loadTableData();
                                }
                            })
                        }
                    }
                );
            },

            // 点击新建
            showCreate() {
                var vm = this;
                vm.actionType = '新建';
                vm.dialogVisible = true;
                vm.dialogTitle = '新建菜品类型';
            },

            // 点击编辑
            showEdit(index, row) {
                var vm = this;
                // 获取科室信息
                vm.getSingle(row.id).then(() => {
                    vm.actionType = '编辑';
                    vm.dialogTitle = "编辑菜品类型";
                    vm.dialogVisible = true;
                })
            },

            // 获取菜品类型
            getSingle(id) {
                var vm = this;
                return new Promise(resolve => {
                    abp.ajax({
                        type: 'GET',
                        url: `//api/food-type/get-detail?id=${id}`
                    }).then(res => {
                        if (res.code === 0) {
                            vm.foodType = res.data;
                        }
                        resolve();
                    })
                })
            },

            // 保存
            saveFoodType() {
                var vm = this;
                if (vm.actionType=== '新建') {
                    vm.createSubmit();
                }
                else { // 编辑
                    vm.editSubmit();
                }
            },

            // 创建提交
            createSubmit() {
                var vm = this;
                dep = vm.department;
                vm.$refs['foodTypeForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'POST',
                            data: JSON.stringify(vm.foodType),
                            url: '/api/food-type/create'
                        }).then((res) => {
                            if (res.code === 0) {
                                abp.notify.success(res.msg)
                                vm.resetForm();
                                vm.loadTableData();
                            }
                        });
                    }
                });
            },

            // 编辑提交
            editSubmit() {
                var vm = this;
                vm.$refs['foodTypeForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'POST',
                            data: JSON.stringify(vm.foodType),
                            url: '/api/food-type/edit'
                        }).then((res) => {
                            if (res.code === 0) {
                                vm.loadTableData();
                                abp.notify.success(res.msg);
                                vm.resetForm();
                            }
                        });
                    }
                });
            },

            // 表单重置
            resetForm() {
                var vm = this;
                vm.$refs['dialogForm'].resetFields();
                vm.foodType.id = null;
                vm.foodType.name = null;
                vm.foodType.description = null;
            }
        },
        created() {
            var vm = this;
            vm.loadTableData();
        },
        mounted() {

        }
    });
})